package com.farmpost.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farmpost.server.mapper.ResourceMapper;
import com.farmpost.server.pojo.Resource;
import com.farmpost.server.pojo.RespBean;
import com.farmpost.server.service.IResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 资源表 服务实现类
 * </p>
 *
 * @author king
 * @since 2022-05-03
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements IResourceService {

    @Autowired
    private ResourceMapper resourceMapper;
    /**
     * 推荐资源
     * @param resource
     * @return
     */
    @Override
    public RespBean insertResource(Resource resource) {
        resource.setDateTime(LocalDateTime.now());
        int insert = resourceMapper.insert(resource);
        if (insert == 1) {
            return RespBean.success("资源推荐成功");
        }
        return RespBean.error("系统错误，资源推荐失败");
    }

    /**
     * 编辑资源
     * @param resource
     * @return
     */
    @Override
    public RespBean updateResource(Resource resource) {
        int updateResource = resourceMapper.updateResource(resource.getId(), resource.getResourceName(), resource.getResourceLink(), resource.getOther(), resource.getType(), LocalDateTime.now());
        if (updateResource == 1)
            return RespBean.success("资源编辑成功");
        return RespBean.error("资源编辑失败");
    }

    /**
     * 删除资源
     * @param id
     * @return
     */
    @Override
    public RespBean deleteResource(Integer id) {
        int delete = resourceMapper.delete(new QueryWrapper<Resource>().eq("id", id));
        if (delete == 1)
            return RespBean.success("删除资源成功");
        return RespBean.error("删除资源失败");
    }

    /**
     * 查看资源
     * @param resourcePage
     * @return
     */
    @Override
    public List<Resource> getResources(Page<Resource> resourcePage) {
        QueryWrapper<Resource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        Page<Resource> selectPage = resourceMapper.selectPage(resourcePage, queryWrapper);
        return selectPage.getRecords();
    }
}
